package com.example.springboot;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.beetl.core.resource.WebAppResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.beetl.sql.core.ClasspathLoader;
import org.beetl.sql.core.DefaultNameConversion;
import org.beetl.sql.core.Interceptor;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.spring4.BeetlSqlDataSource;
import org.beetl.sql.ext.spring4.BeetlSqlScannerConfigurer;
import org.beetl.sql.ext.spring4.SqlManagerFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@SpringBootApplication
public class SpringbootApplication{

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}

	@Bean
	public TomcatServletWebServerFactory servletContainer() {
		TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
		tomcatServletWebServerFactory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
            Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
            protocol.setMaxConnections(200);
            protocol.setMaxThreads(200);
            protocol.setSelectorTimeout(3000);
            protocol.setSessionTimeout(3000);
            protocol.setConnectionTimeout(3000);
        });
		tomcatServletWebServerFactory.addAdditionalTomcatConnectors(httpConnector());

		return tomcatServletWebServerFactory;
	}

	@Bean
	public Connector httpConnector() {
		Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
		connector.setScheme("http");
		//Connector监听的http的端口号
		connector.setPort(8080);
		connector.setSecure(false);
		//监听到http的端口号后转向到的https的端口号
		connector.setRedirectPort(8081);
		return connector;
	}


	//=============以下是beetl配置=========================
//	@Bean(initMethod = "init", name = "beetlConfig")
//	public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
//		BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
//		ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
//		// WebAppResourceLoader 配置root路径是关键
//		//WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(patternResolver.getResource("classpath:/sql").getFile().getPath());
//		WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader();
//		beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader);
//
//		//读取配置文件信息
//		return beetlGroupUtilConfiguration;
//	}

//	@Bean(name = "beetlViewResolver")
//	public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
//		BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
//		beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
//		beetlSpringViewResolver.setOrder(0);
//		beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
//		return beetlSpringViewResolver;
//	}

	//=============================以下是beetsql配置=============
	//配置包扫描
//	@Bean(name = "beetlSqlScannerConfigurer")
//	public BeetlSqlScannerConfigurer getBeetlSqlScannerConfigurer() {
//		BeetlSqlScannerConfigurer conf = new BeetlSqlScannerConfigurer();
//		conf.setBasePackage(" com.example.demo.dao");
//		conf.setDaoSuffix("Dao");
//		conf.setSqlManagerFactoryBeanName("sqlManagerFactoryBean");
//		return conf;
//	}

//	@Bean(name = "sqlManagerFactoryBean")
//	@Primary
//	public SqlManagerFactoryBean getSqlManagerFactoryBean(@Qualifier("datasource") DataSource datasource) {
//		SqlManagerFactoryBean factory = new SqlManagerFactoryBean();
//		BeetlSqlDataSource source = new BeetlSqlDataSource();
//		source.setMasterSource(datasource);
//		factory.setCs(source);
//		factory.setDbStyle(new MySqlStyle());
//		factory.setInterceptors(new Interceptor[]{new DebugInterceptor()});
//		factory.setNc(new DefaultNameConversion());
//		factory.setSqlLoader(new ClasspathLoader("/sql"));
//		//sql文件路径
//		return factory;
//	}

	//开启事务
	@Bean(name = "transactionManager")
	public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("datasource") DataSource datasource) {
		DataSourceTransactionManager dsm = new DataSourceTransactionManager();
		dsm.setDataSource(datasource);
		return dsm;
	}

}
